﻿import QtQuick 2.3
import QtQuick.Controls 1.2
import QmlObjectTest 1.0
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls.Private 1.0

ApplicationWindow {
    id: window
    visible: true
    width: Screen.width
    height: Screen.height
    title: qsTr("大转盘")

    property real value: 0
    property int result: -1

    function addZero(value){
        if(value < 10){
            return "0" + value
        }
        return value
    }

    function getSeed(){

        var date = new Date
        var y = date.getFullYear()
        var M = addZero(date.getMonth())
        var d = addZero(date.getDate())
        var h = addZero(date.getHours())
        var m = addZero(date.getMinutes())
        var s = addZero(date.getSeconds())

        var time = String(y) + String(M) + String(d) + String(h) + String(m) + String(s)

        return Number(time)
    }



    Image{
        id: imgZhuan
        source: "qrc:/img/zhuan.png"
        anchors.horizontalCenter: parent.horizontalCenter
        y: 200

        Image{
            id: imgZhen
            source: "qrc:/img/zhen.png"
            x: (parent.width-width)/2
            y: (parent.height-height)/2

            transform: Rotation{
                id: rotation
                axis.x: 0
                axis.y: 0
                axis.z: 1
                origin.x: imgZhen.width/2
                origin.y: imgZhen.height/2
            }
        }

        MouseArea{
            id: mouseArea
            anchors.fill: parent
            onClicked: {
                if(animRotaion.running) return


                var s = getSeed()
                Math.seed = s

                value = rotation.angle + Math.floor(Math.random()*360 + 360*1)

                animRotaion.restart()
            }
        }
    }

    Image{
        id: imgResult
        source: "qrc:/img/" + String(result) + ".png"
        anchors.top: imgZhuan.bottom
        anchors.topMargin: 100
        anchors.horizontalCenter: parent.horizontalCenter
        width: 320
        height: 320
        fillMode: Image.Stretch
        opacity: 0

    }

    SequentialAnimation{
        id: animRotaion
        PropertyAnimation{target: rotation; property: "angle"; to: value; duration: value / 360 * 1000}

        onStarted: {
            imgResult.opacity = 0
        }

        onStopped: {
           value = value % 360
            rotation.angle = rotation.angle % 360
            console.log(value)

            if(value <= 30){
                //yellow
                result = 1
            }
            else if(value <= 90){
                result = 2
            }
            else if(value <= 120){
                result = 3
            }
            else if(value <= 180){
                result = 4
            }
            else if(value <= 210){
                result = 5
            }
            else if(value <= 270){
                result = 6
            }
            else if(value <= 330){
                result = 7
            }
            else{
                result = 1
            }

            console.log(result)

            animResult.restart()
        }
    }

    SequentialAnimation{
        id: animResult
        PropertyAnimation{target: imgResult; property: "opacity"; from: 0; to: 1; duration: 500}
        onStarted: {
            mouseArea.enabled = false
        }
        onStopped: {
            mouseArea.enabled = true
        }
    }
}

